﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;
using System.IO;

namespace HY.WEB.OA
{
    public partial class download : PageBase
    {
        public string us,sid;
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();

            if (us == null || us == "")
            {
                Alert.Show("登陆超时");
                Response.End();
            }

            if (!String.IsNullOrEmpty(Request["sid"]))
            {
                sid = Request["sid"].Trim();              
            }
            else
                return;

            down(sid);
        }

        void down(string sid)
        {
            string path = "";

            DataTable dts=BLL.msg.msgbase.get_list(sid);

            if (dts.Rows.Count>0)
            path = dts.Rows[0]["sfile"].ToString().Trim();
            
            if (path == "")
            {
                Response.Write("未找到相应文件!");
                return;
            }

            string fileName = Path.GetFileName(path); ;//客户端保存的文件名
            string filePath = Server.MapPath(path);//路径

            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();

        }
    }
}